/**
  thread-utils.c - Created by Timothy Morey on 5/27/2012
*/

#ifdef __WINDOWS__
#include <Windows.h>
#else
#include <unistd.h>
#endif

#include "err.h"
#include "thread-utils.h"

int IsCurrentThread(Thread thread, int* iscurrent)
{
  int retval = NVN_NOERR;

  if(iscurrent)
  {
#ifdef __WINDOWS__
    *iscurrent = GetCurrentThreadId() == thread.Id;
#else
    *iscurrent = pthread_equal(thread.Handle, pthread_self());
#endif
  }
  else
  {
    retval = NVN_EINVARGS;
  }

  return retval; 
}

int ThreadSleep(int mili)
{
  int retval = NVN_NOERR;

#ifdef __WINDOWS__
  Sleep(mili);
#else
  usleep(mili);
#endif

  return retval;
}

int ThreadYield()
{
  int retval = NVN_NOERR;

#ifdef __WINDOWS__
  SwitchToThread();
#else
  pthread_yield();
#endif

  return retval;
}
